import request from '@/utils/request'
import qs from 'qs'

export const createRole = (data: any) =>
  request({
    url: '/admin/role',
    method: 'post',
    data
  })

export const deleteRole = (id: number) =>
  request({
    url: `/admin/role/${id}`,
    method: 'delete'
  })

export const updateRole = (id: number, data: any) =>
  request({
    url: `/admin/role/${id}`,
    method: 'put',
    data
  })

export const getRole = (id: number) =>
  request({
    url: `/admin/role/${id}`,
    method: 'get'
  })

export const getRoles = (params: any) =>
  request({
    url: '/admin/role/roles',
    method: 'get',
    params
  })

// 获取角色权限Ids
export const getByRolePermission = (roleId: number) =>
  request({
    url: `/admin/role/${roleId}/menuIds`,
    method: 'get'
  })

// 更新角色权限
export const updateRolePermission = (roleId: number, menuIds: number[]) =>
  request({
    url: `/admin/role/${roleId}/menuIds`,
    method: 'put',
    params: {
      menuIds
    },
    paramsSerializer: params => {
      return qs.stringify(params, { indices: false })
    }
  })
